﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Sistema_de_Taller.SDT_DAL.Utilities
{
    public class Replacement_DAL
    {
        public void ReplacementInsert(Common_Files.Definitions.Remplacement Replacement)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlCommand Command = new SqlCommand(@"insert into Replacement (Replacement_ID, Replacement_Name, Replacement_Brand, Replacement_Stock, Replacement_Output, Replacement_Location) 
                                                                values ('" + Replacement.Replacement_ID + "', '" + Replacement.Replacement_Name + "', '" + Replacement.Replacement_Brand + "','" + Replacement.Replacement_Stock + "','" + Replacement.Replacement_Output + "', '" + Replacement.Replacement_Location + "')", Connection);
            Common_Files.Connection.CloseConecction(Connection, Command);
        }

        public bool ReplacementIdExist(UInt32 ID)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select Replacement_ID as 'ID', Replacement_Name as 'Nombre', Replacement_Brand as 'Marca', Replacement_Stock as 'Stock', Replacement_Output as 'Cantidad Solicitada', Replacement_Location as 'Locación' from Replacement where Replacement_ID = '" + ID + "'", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            if (DataSet.Tables[0].Rows.Count > 0) return true;
            else return false;        
        }

        public bool ReplacementStockExist(UInt32 ID, UInt32 UsedQuantity)
        {
            //EL @ es para dividir una línea de código en 2
            Int32 Resta = Convert.ToInt32(ReplacementActualStock(ID.ToString().Trim())) - Convert.ToInt32(UsedQuantity);
            if (Resta >= 0) return true;
            else return false;
        }

        public DataSet ReplacementIdLoad(String ID)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select Replacement_ID as 'ID', Replacement_Name as 'Nombre', Replacement_Brand as 'Marca', Replacement_Stock as 'Stock', Replacement_Output as 'Cantidad Solicitada', Replacement_Location as 'Locación' from Replacement where Replacement_ID = '" + ID + "'", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);  
        }

        public DataSet ReplacementLoad()
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select Replacement_ID as 'ID', Replacement_Name as 'Nombre', Replacement_Brand as 'Marca', Replacement_Stock as 'Stock', Replacement_Output as 'Cantidad Solicitada', Replacement_Location as 'Locación' from Replacement", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);
        }

        public void ReplacementChange(Common_Files.Definitions.Remplacement Replacement)
        {
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlCommand Command = new SqlCommand("update Replacement set Replacement_Name= '" + Replacement.Replacement_Name + "' , Replacement_Brand = '" + Replacement.Replacement_Brand + "', Replacement_Stock = '" + Replacement.Replacement_Stock + "', Replacement_Output = '" + Replacement.Replacement_Output + "' , Replacement_Location = '" + Replacement.Replacement_Location + "' where Replacement_ID = '" + Replacement.Replacement_ID + "' ", Connection);
            Common_Files.Connection.CloseConecction(Connection, Command);
        }

        public void ReplacementStockUpdate(Common_Files.Definitions.Remplacement Replacement)
        {
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlCommand Command = new SqlCommand("update Replacement set Replacement_Stock = '" + Replacement.Replacement_Stock + "', Replacement_Output = '" + Replacement.Replacement_Output + "' where Replacement_ID = '" + Replacement.Replacement_ID + "' ", Connection);
            Common_Files.Connection.CloseConecction(Connection, Command);
        }

        public UInt32 ReplacementActualStock(String ID)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select Replacement_Stock as 'Stock' from Replacement where Replacement_ID = '" + ID + "'", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return Convert.ToUInt32(DataSet.Tables[0].Rows[0].ItemArray[0].ToString().Trim());
        }

        public void ReplacementDelete()
        {
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            //  SqlCommand Command = new SqlCommand("delete from Replacement where mov_codigo = '" + DgvDatos.SelectedRows[0].Cells[0].Value.ToString() + "'", Connection);
            //  CloseConecction(Connection, Command);
        }

        public DataSet ReplacementSearch(String Parameter, String Column)
        {
            String query = string.Format("Select Replacement_Name as 'Nombre', Replacement_Brand as 'Marca', Replacement_Stock as 'Stock', Replacement_Output as 'Cantidad Solicitada', Replacement_Location as 'Locación' from Replacement where {0} like '%" + Parameter + "%'", Column);
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(query, Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);
        }


    }
}